package com.zc.im.controller;

import com.zc.im.model.im.group.ZcImDirectory;
import com.zc.im.model.im.group.ZcImDirectoryGroup;
import com.zc.im.service.im.group.ImDirectoryService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/13.
 */
@Controller
@RequestMapping("/directory")
public class ImDirectoryController {

    private Logger log = LoggerFactory.getLogger(ImDirectoryController.class);

    @Autowired
    private ImDirectoryService directoryService;


    @RequestMapping("/insertDirectory")
    @ResponseBody
    public ResponseJson insertDirectory(String directoryStr){
        ResponseJson responseJson = new ResponseJson();

        if(StringUtils.isEmpty(directoryStr)){
            return responseJson;
        }
        ZcImDirectory directory = JsonUtils.strToClass(directoryStr, ZcImDirectory.class);
        if(directory == null){
            log.error("数据异常，directoryStr{}", directoryStr);
            return responseJson;
        }
        Map<String, Object> dataMap = new HashMap<>();
        Integer key = directoryService.insertDirectory(directory);
        dataMap.put("directoryKey", key);
        return responseJson;
    }



    @RequestMapping("/insertDirectoryGroup")
    @ResponseBody
    public ResponseJson insertDirectoryGroup(String directoryGroupStr){
        ResponseJson responseJson = new ResponseJson();
        if(StringUtils.isEmpty(directoryGroupStr)){
            return responseJson;
        }
        ZcImDirectoryGroup directoryGroup = JsonUtils.strToClass(directoryGroupStr, ZcImDirectoryGroup.class);
        if(directoryGroup == null){
            return responseJson;
        }
        Integer key = directoryService.insertDirectoryGroup(directoryGroup);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("directoryGroupKey", key);
        return responseJson;
    }

    @RequestMapping("/insertDefDirectoryGroup")
    @ResponseBody
    public ResponseJson insertDefDirectoryGroup(Integer userId){
        ResponseJson responseJson = new ResponseJson();
        if(userId == null || userId ==0){
            return responseJson;
        }
        String key = directoryService.insertDefDirectoryGroup(userId);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("defDirectoryGroupKey", key);
        return responseJson;
    }

    public static void main(String[] args) {
        String string = "aaa456ac";

        //查找指定字符是在字符串中的下标。在则返回所在字符串下标；不在则返回-1.
        System.out.println(string.indexOf("b"));//indexOf(String str)；返回结果：-1，"b"不存在

    }
}
